/**
 * @file    app_control.c
 * @author  yhy
 * @brief   控制模块
 * @version 1.0
 * @date    2025-07-24
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "app_control.h"

#include "drv_timer.h"
#include "inf_pump.h"
#include "inf_solenoid_valve.h"

static result_e app_control_solenoid_valve_config(void);
static void     app_control_valve_high_side_switch_handle(void);
static void     app_control_pump_high_side_switch_handle(void);

/**
 * @brief 初始化控制模块
 *
 * @return result_e 初始化结果, 初始化成功返回RESULT_STATUS_OK, 初始化失败返回其他错误码
 */
result_e app_control_init(void)
{
    result_e result = RESULT_STATUS_OK;

    /* 配置电磁阀系统 */
    result = app_control_solenoid_valve_config();
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    /* 初始化高测开关, 管理VALVE+和PUMP+和FAN */
    result = inf_bts7020_init();
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    // /* 开启VALVE开关 */
    // app_control_switch(BTS7020_DEVICE_PUMP_VALVE, SWITCH_TYPE_VALVE, true);
    // /* 开启PUMP+ */
    // app_control_switch(BTS7020_DEVICE_PUMP_VALVE, SWITCH_TYPE_PUMP, true);
    /* 开启FANL */
    app_control_switch(BTS7020_DEVICE_FAN, SWITCH_TYPE_FANL, true);

    /* 初始化气泵 */
    result = inf_pump_init();
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    return result;
}

/**
 * @brief 控制高策开关输入通道的开关, VALVE+_EN-->+12_VALVE, PUMP+_EN-->PUMP+
 *
 * @param device        高测开关设备
 * @param switch_type   高测开关类型
 * @param enable        true: 开启, false: 关闭
 * @return  result_e 控制结果, 控制成功返回RESULT_STATUS_OK, 控制失败返回其他错误码
 */
result_e app_control_switch(bts7020_device_e device, switch_type_e switch_type, bool enable)
{
    result_e result = RESULT_STATUS_OK;

    bts7020_in_e bts_in = (bts7020_in_e)switch_type;

    result = inf_bts7020_set_switch(device, bts_in, enable);

    return result;
}

/**
 * @brief 控制模块处理函数，放在调度器中循环调度
 *
 */
void app_control_handle(void)
{
    app_control_pump_high_side_switch_handle();
    app_control_valve_high_side_switch_handle();
}

/**
 * @brief 处理电磁阀高侧开关开启or关闭
 *
 */
static void app_control_valve_high_side_switch_handle(void)
{
    /* 判断电磁阀高侧是否需要开启 */
    bool valve_enable = false;
    for (uint8_t i = 0; i < SOLENOID_VALVE_CH_MAX; ++i) {
        if (valve_system.valve_list[i].state != SOLENOID_VALVE_STATE_DISABLED) {
            valve_enable = true;
            break;
        }
    }

    /* 根据判断结果控制VALVE+开关 */
    app_control_switch(BTS7020_DEVICE_PUMP_VALVE, SWITCH_TYPE_VALVE, valve_enable);
}

/**
 * @brief 处理气泵高侧开关开启or关闭
 *
 */
static void app_control_pump_high_side_switch_handle(void)
{
    /* 判断气泵高侧是否需要开启 */
    pump_state_e pump_state  = inf_pump_get_state();
    bool         pump_enable = (pump_state != PUMP_STATE_IDLE);

    /* 根据气泵状态控制PUMP+开关 */
    app_control_switch(BTS7020_DEVICE_PUMP_VALVE, SWITCH_TYPE_PUMP, pump_enable);
}

/**
 * @brief 配置电磁阀系统
 *
 * @return result_e 配置结果, 配置成功返回RESULT_STATUS_OK, 配置失败返回其他错误码
 */
static result_e app_control_solenoid_valve_config(void)
{
    result_e result = RESULT_STATUS_OK;

    /* 初始化电磁阀系统 */
    result = inf_solenoid_valve_init(); // 内部已初始化移位寄存器
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    /* 配置定时器1, 用于控制电磁阀, 5ms调度一次 */
    snf_timer_config_t timer_config = {
        .instance      = TIMER_INSTANCE_TIMER1,
        .prescaler     = 30U - 1U,
        .compare_value = 5000U - 1U,
        .callback      = inf_solenoid_valve_ctrl_handle,
    };
    result = snf_drv_timer_init(&timer_config);
    if (result != RESULT_STATUS_OK) {
        return result;
    }

    return result;
}
